package com.example.bang.computer;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private Button[] buttons=new Button[17];
    private int[] ids=new int[]{R.id.bt1,R.id.bt2,R.id.bt3,R.id.bt4,R.id.bt5,R.id.bt6,R.id.bt7,
            R.id.bt8,R.id.bt9,R.id.bt10,R.id.bt11,R.id.bt12,R.id.bt13,R.id.bt14,R.id.bt15,R.id.bt16,R.id.bt17
    };
    private TextView textView;
    private String expression="";
    //判断运算是否结束
    private boolean end=false;
    //判断是否存在小数点
    private boolean point=false;
    //判断是否存在符号
    private boolean operation=false;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        for(int i=0;i<ids.length;i++){
            buttons[i]=(Button)findViewById(ids[i]);
            buttons[i].setOnClickListener(this);
        }
        textView=(TextView)findViewById(R.id.contentText);
    }

    //判断输入是否符合规范
    public void onClick(View view){
        //获取被点击按钮的id
        int id =view.getId();
        Button button =(Button)view.findViewById(id);
        String current= button.getText().toString();
        if(end){
            expression="";
            end=false;
            point=false;
            operation=false;
        }

        if(current.equals("清除")){
            expression="0";
            end=true;
            point=false;
            operation=false;
        }
        else if (current.equals("+")||current.equals("-")||current.equals("*")||current.equals("/")){
            if(!operation){
                //表达式为空,
                if(expression.isEmpty()){
                    //输入为正负号,添加进表达式,但不改变符号状态
                    if (current.equals("-") || current.equals("+")) {
                        expression += current;
                    }
                    //输入为乘除号, 不执行操作
                    if (current.equals("*") || current.equals("/")) {
                    }
                }
                // 表达式不为空,且表达式最后位是数字
                else if(expression.charAt(expression.length()-1)>='0'&&expression.charAt(expression.length()-1)<='9'){
                    expression+=current;
                    operation=true;
                }
                point=false;

            }
            else{ //已经存在运算符
                //表达式最后一位是数字
                if(expression.charAt(expression.length()-1)>='0'&&expression.charAt(expression.length()-1)<='9'){
                    double result= count();
                    if(result==Double.MAX_VALUE){
                        expression="错误";
                    }else{
                        //保留两位小数
                        result =(double)Math.round(result*100)/100;
                        expression=""+result;
                        expression+=current;
                        point=false;
                    }
                }
            }
        }
        else if(current.equals("=")){
            //存在符号,且表达式最后一位是数字
            if(operation&&expression.charAt(expression.length()-1)>='0'&&expression.charAt(expression.length()-1)<='9'){
                double result= count();
                if(result==Double.MAX_VALUE){
                    expression="错误";
                }else{
                    //保留两位小数
                    result =(double)Math.round(result*100)/100;
                    expression=""+result;
                    end=true;
                }
            }
        }
        else if(current.equals(".")){
            if(!point){
                //如果表达式为空 或者表达式最后位不是数字, 输入. 默认加上0
                if(expression.equals("")||(expression.charAt(expression.length()-1)<'0'||expression.charAt(expression.length()-1)>'9')){
                    expression+="0";
                }
                // 存在符号 同时表达式最后位不是数字
                else if(operation&& (expression.charAt(expression.length()-1)<'0'||expression.charAt(expression.length()-1)>'9')){
                    expression+="0";
                }
                expression+=current;
                point=true;
            }
        }
        else{ //此时为数字
            if(end){
                expression="";
            }
            expression+=current;
        }

        textView.setText(expression);
    }

    private double count(){
        double result=0;
        double num1,num2;
        String str1,str2;

        int operateIndex=0;
        char operate='0';
        for(int i=0;i<expression.length();i++){
            //找出表达式的运算符
            if(expression.charAt(i)=='+'||expression.charAt(i)=='-'||expression.charAt(i)=='*'||expression.charAt(i)=='/'){
                operateIndex=i;
                operate=expression.charAt(i);
            }
        }
        str1=expression.substring(0,operateIndex);
        str2=expression.substring(operateIndex+1,expression.length());

        num1=Double.parseDouble(str1);
        num2=Double.parseDouble(str2);

        if(operate=='+'){
            result=num1+num2;
        }
        else if(operate=='-'){
            result=num1-num2;
        }
        else if(operate=='*'){
            result=num1*num2;
        }
        else if(operate=='/'){
            if(num2==0){//返回最大值,作为判断错误的依据(待改进)
                return Double.MAX_VALUE;
            }
            else{
                result=num1/num2;
            }
        }
        return result;
    }
}